package baidu.ui.managers
{
    import flash.system.*;
    import flash.utils.*;

    public class InstanceManager extends Object
    {
        private static var instances:Dictionary = new Dictionary();

        public function InstanceManager()
        {
            return;
        }

        public static function createInstance(name:*):*
        {
            if (name is String)
            {
                name = getClass(name);
            }
            if (name is Class)
            {
                return new name;
            }
            return name;
        }

        public static function createSingletonInstance(name:*):*
        {
            return createUniqueInstance(name, "singleton");
        }

        public static function createUniqueInstance(name:*, mode:String):*
        {
            if (name is String)
            {
                name = getClass(name);
            }
            if (name is Class)
            {
                if (instances[name] == null)
                {
                    instances[name] = new Dictionary();
                }
                if (instances[name][mode] == null)
                {
                    instances[name][mode] = createInstance(name);
                }
                return instances[name][mode];
            }
            return name;
        }

        public static function getClass(className:String):Class
        {
            return ApplicationDomain.currentDomain.getDefinition(className) as Class;
        }

    }
}
